<?php

if ( !class_exists( "BME_sequence" ) ) {
require_once("Object.php");

class BME_sequence extends BME_object {
	
	var $dataset;		// int	ID of Dataset on Lcl-DB.
	var $description;	// str	Description of the sequence.
	var $sequence;		// str	The sequence itself.
	var $gen;		// str	The gene name.
	var $defline_prefix;	// str	A prefix on defline, before ID and after >.
	
	/**
	 * bme_sequence new bme_sequence([mix id[, int version]]);
	 * @param mix id (optional): The ID on Lcl-DB (int), or the ID.version (str or float).
	 * @param int version (optional): The version of the sequence.
	 * @return bme_sequence (object).
	 */
	function BME_sequence($id=NULL,$version=NULL) {
		parent::__construct($id,$version);
		$this->defline_prefix=BME_3L_NAME."|lcl|";
	}
	/**
	 * bool load(int id[, int version]);
	 * @param int id.
	 * @param int version (optional): Last version by default.
	 * @return bool: success.
	 */
	function load($id,$version=NULL){
		$this->_parseId($id,$version);
		$out=false;
		$ver="";
		if(is_int($version)) $ver=" AND `version`='$version'";
		$this->openDB();
		$res=$this->DBquery("select * from `Sequence` where `idSequence`='$id' $ver ORDER BY `version` DESC");
		if($row=$this->DBfetch_assoc($res)){
			$this->id = (int)$row["idSequence"];
			$this->version = (int)$row["version"];
			$this->source = $row["source"];
			$this->dataset = (int)$row["Dataset_idDataset"];
			$this->description = $row["description"];
			$this->sequence = $row["sequence"];
			$this->gen = $row["gen"];
			$out=true;
		}
		$this->closeDB();
		return $out;
	}
	/**
	 * int getdataset(void);
	 * @return int: Dataset ID.
	 */
	function getDataset(){
		assert('isset($this->id)');
		return $this->dataset;
	}
	/**
	 * str getdescription(void);
	 * @return str: Description.
	 */
	function getDescription(){
		assert('isset($this->id)');
		return trim($this->description);
	}
	/**
	 * str getdefline(void);
	 * @return str: Definition line.
	 */
	function getDefline(){
		assert('isset($this->id)');
		return $this->defline_prefix.$this->getId()." ".$this->getDescription();
	}
	/**
	 * str getsequence([int wrap]);
	 * @param int wrap: Wrap at $wrap chars.  0 (no wrap) by default.
	 * @return str: Sequence.
	 */
	function getSequence($wrap=0){
		assert('isset($this->id)');
		$wrap=(int)$wrap;
		return ($wrap?wordwrap($this->sequence,$wrap,"\n",true):$this->sequence);
	}
	/**
	 * str getgen(void);
	 * @return str: Gene Name.
	 */
	function getGen(){
		assert('isset($this->id)');
		return array_shift($this->getFeatures("gene"));
	}
	/**
	 * arr getfeatures(str feature[, mix which, [, int limit]]);
	 * @param str feature: Which feature?
	 * @param mix which: Required by some features (Which BLAST if $feature=BLAST, for example).
	 * @param int limit (optional): How many?, all by default.
	 */
	function getFeatures($feature,$which=NULL,$limit=NULL){
		assert('isset($this->id)');
		$out=array();
		switch(strtolower($feature)){
		case 'blast':
			require_once(BME_ROOT."core/Blast.php");
			$this->openDB();
			$res=$this->DBquery("SELECT `idBlastResult` from `BlastResult`"
			." where `queryID`='".$this->DBescape($this->defline_prefix.$this->getId())."'"
			.($which?" and `BlastParam_idBlastParam`='$which'":"")
			." order by `hspEvalue` ASC"
			.($limit?" limit $limit":""));
			while($row=$this->DBfetch_assoc($res)){
				$out[]=new BME_Blast((int)$row['idBlastResult']);
			}
			$this->closeDB();
		break;
		case 'pattern':
			require_once(BME_ROOT."core/Pattern.php");
			$this->openDB();
			$res=$this->DBquery("SELECT `idPatternResult` from `PatternResult`"
			." where `Sequence_idSequence`='".$this->getId()."'"
			.($which?" and `patternName`='$which'":"")
			.($limit?" limit $limit":""));
			while($row=$this->DBfetch_assoc($res)){
				$out[]=new BME_Pattern((int)$row['idPatternResult']);
			}
			$this->closeDB();
		break;
		case 'gene':
			$out[]=$this->gen;
		break;
		}
		return $out;
	}
	/**
	 * arr getall(void);
	 * @return arr: Arr of int, array of IDs.
	 */
	function getAll(){
		return $this->search();
	}
	/**
	 * arr search([arr param]);
	 * @param arr param (optional): Search parameters.  If any, an alias of $this->getAll(), any by default.
	 * @return arr: Arr of int, array of IDs.
	 */
	function search($param=array()){
		$equiv_field=array("dataset"=>"Dataset_idDataset", "gene"=>"gen", "description"=>"description");
		$approx_search=array("description");
		$search = "select max(`version`),`idSequence` from `Sequence` where 1";
		foreach($param as $k=>$v){
			if($v && $equiv_field[$k]){
				if(in_array($k,$approx_search)) $search.= " AND `{$equiv_field[$k]}` LIKE '%$v%'";
				else $search.= " AND `{$equiv_field[$k]}`='$v'";
			}
		}
		$search.= " group by `idSequence`";
		$out=array();
		$this->openDB();
		$res=$this->DBquery($search);
		while($row=$this->DBfetch_assoc($res)){
			$out[]=$row["idSequence"].".".$row["version"];
		}
		$this->closeDB();
		return $out;
	}
}

}


?>
